<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use addons\epay\library\Service;
use app\common\library\Sms;
use Exception;

/**
 *  收货/发货
 */
class Receive extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];




    /**
     * @ApiTitle    (选择商品游戏区服)
     * @ApiSummary  (选择商品游戏区服)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/constituency)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="游戏id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    'id':1

     * })
     */
    public function constituency()
    {
        $id = $this->request->post('id');
        $res = Db::name('game')->field('id,game,type')->where('id',$id)->find();
        if ($res['type'] == 'mobile') {
            $res['plat'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',1)->where('status','normal')->select();
            foreach ($res['plat'] as $k=>$v) {
                $res['plat'][$k]['type'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',5)->where('status','normal')->where('is_num',1)->select();
                $res['plat'][$k]['operator'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',2)->where('status','normal')->select();
                foreach ($res['plat'][$k]['operator'] as $kk=>$vv) {
                    $res['plat'][$k]['operator'][$kk]['area'] = Db::name('game_info')->field('id,pid,name')->where('pid',$vv['id'])->where('gt_id',3)->where('status','normal')->select();
                    foreach ($res['plat'][$k]['operator'][$kk]['area'] as $key=>$val) {
                        $res['plat'][$k]['operator'][$kk]['area'][$key]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$val['id'])->where('gt_id',4)->where('status','normal')->select();
                    }
                }
            }
        } elseif ($res['type'] == 'network') {
            $res['type'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',5)->where('status','normal')->where('is_num',1)->select();
            $res['area'] = Db::name('game_info')->field('id,pid,name')->where('game_id',$id)->where('gt_id',3)->where('status','normal')->select();
            foreach ($res['area'] as $k=>$v) {
                $res['area'][$k]['server'] = Db::name('game_info')->field('id,pid,name')->where('pid',$v['id'])->where('gt_id',4)->where('status','normal')->select();
            }
        }

        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (收货添加展示)
     * @ApiSummary  (收货添加展示)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/receiving)
     * @ApiParams   (name="type", type="varchar", required=true, description="物品类型id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": "万金"
     * })
     */
    public function receiving()
    {
        $type = $this->request->post('type');
        $typeName = Db::name('game_info')->where('id',$type)->value('name');
        $res = Db::name('attr')->where('type',$typeName)->value('value');
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (添加收货商品)
     * @ApiSummary  (添加收货商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/receiveAdd)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiParams   (name="title", type="varchar", required=true, description="商品标题")
     * @ApiParams   (name="area_id", type="varchar", required=true, description="游戏区id")
     * @ApiParams   (name="server_id", type="varchar", required=true, description="服务器id")
     * @ApiParams   (name="goods_type", type="varchar", required=true, description="物品类型id")
     * @ApiParams   (name="num", type="varchar", required=true, description="收货数量")
     * @ApiParams   (name="price", type="varchar", required=true, description="收货单价")
     * @ApiParams   (name="ask", type="varchar", required=true, description="出货要求")
     * @ApiParams   (name="min", type="varchar", required=true, description="最小收货数量")
     * @ApiParams   (name="max", type="varchar", required=true, description="最大收货数量")
     * @ApiParams   (name="specs", type="varchar", required=true, description="单位")
     * @ApiParams   (name="mobile", type="varchar", required=true, description="手机号")
     * @ApiParams   (name="qq", type="varchar", required=true, description="qq")
     * @ApiParams   (name="jy_time", type="varchar", required=true, description="交易时间 每天几点-几点")
     * @ApiParams   (name="coin_type", type="varchar", required=true, description="出货方式 （1 邮寄交易 2 拍卖交易 3 面对面交易）")
     * @ApiParams   (name="role_account", type="varchar", required=true, description="收货角色")
     * @ApiParams   (name="role_level", type="varchar", required=true, description="收货等级")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "发布成功！",
    "time": "1627553755",
    "data": {

    }
     * })
     */
    public function receiveAdd()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token', $token)->value('id');
        $user = Db::name('personal')->where('id', $id)->find();
        $gameId = $this->request->post('id');
        $title = $this->request->post('title');
        $area_id = $this->request->post('area_id');
        $server_id = $this->request->post('server_id');
        $goods_type = $this->request->post('goods_type');
        $num = $this->request->post('num');
        $price = $this->request->post('price');
        $ask = $this->request->post('ask');
        $min = $this->request->post('min');
        $specs = $this->request->post('specs');
        $max = $this->request->post('max');
        $mobile = $this->request->post('mobile');
        $qq = $this->request->post('qq');
        $jy_time = $this->request->post('jy_time');
        $coin_type = $this->request->post('coin_type');
        $role_account = $this->request->post('role_account');
        $role_level = $this->request->post('role_level');
        if ($user['is_real'] != 1) {
            $this->error('未实名', [], 101);
        }
        $this->is_login($token);
        $rece_sn = 'O' . $this->getRefundSn($id);
        $typeName = Db::name('game_info')->where('id', $goods_type)->value('name');
        $gameName = Db::name('game')->where('id',$gameId)->value('game');
        $area = Db::name('game_info')->where('id', $area_id)->value('name');
        $server = Db::name('game_info')->where('id', $server_id)->value('name');
        $cross_id = Db::name('game_info')->where('id', $server_id)->value('cross');
        $cross = Db::name('gamecross')->where('id', $cross_id)->value('name');
        $image = Db::name('game')->where('id', $gameId)->value('image');

        $res = [
            'uid'   =>  $id,
            'class_type' => 2,
            'title'   =>  $title,
            'rece_sn'   =>   $rece_sn,
            'game_id'   =>  $gameId,
            'game'  =>  $gameName,
            'area_id'   =>  $area_id,
            'area'  =>  $area,
            'server_id' =>  $server_id,
            'server'    =>  $server,
            'cross_id'    =>  $cross_id,
            'cross'    =>  $cross,
            'goods_type'    =>  $goods_type,
            'type_name' =>  $typeName,
            'image' =>  $image,
            'num'   =>  $num,
            'price' =>  $price,
            'min'   =>  $min,
            'max'   =>  $max,
            'ask'   =>  $ask,
            'specs' =>  $specs,
            'mobile'    =>  $mobile,
            'qq'    =>  $qq,
            'jy_time'   =>  $jy_time,
            'coin_type' =>  $coin_type,
            'role_account' =>  $role_account,
            'role_level' =>  $role_level,
            'createtime'    =>  time(),
            'status'    =>  1
        ];

        $data = Db::name('receiving')->insert($res);
        if ($data) {
            $this->success('发布成功！');
        } else {
            $this->error('发布失败！');
        }

    }



    /**
     * @ApiTitle    (添加发货商品)
     * @ApiSummary  (添加发货商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/qssAdd)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiParams   (name="title", type="varchar", required=true, description="商品标题")
     * @ApiParams   (name="area_id", type="varchar", required=true, description="游戏区id")
     * @ApiParams   (name="server_id", type="varchar", required=true, description="服务器id")
     * @ApiParams   (name="goods_type", type="varchar", required=true, description="物品类型id")
     * @ApiParams   (name="num", type="varchar", required=true, description="收货数量")
     * @ApiParams   (name="price", type="varchar", required=true, description="收货单价")
     * @ApiParams   (name="ask", type="varchar", required=true, description="出货要求")
     * @ApiParams   (name="min", type="varchar", required=true, description="最小收货数量")
     * @ApiParams   (name="max", type="varchar", required=true, description="最大收货数量")
     * @ApiParams   (name="specs", type="varchar", required=true, description="单位")
     * @ApiParams   (name="mobile", type="varchar", required=true, description="手机号")
     * @ApiParams   (name="qq", type="varchar", required=true, description="qq")
     * @ApiParams   (name="coin_type", type="varchar", required=true, description="出货方式 （1 邮寄交易 2 拍卖交易 3 面对面交易）")
     * @ApiParams   (name="invite", type="varchar", required=true, description="推广码")
     * @ApiParams   (name="g_account", type="varchar", required=false, description="游戏账号")
     * @ApiParams   (name="g_password", type="varchar", required=false, description="游戏密码")
     * @ApiParams   (name="role_account", type="varchar", required=true, description="游戏角色名称")
     * @ApiParams   (name="role_level", type="varchar", required=true, description="游戏角色等级")
     * @ApiParams   (name="second_pwd", type="varchar", required=false, description="二级密码")
     * @ApiParams   (name="ware_pwd", type="varchar", required=false, description="仓库密码")
     * @ApiParams   (name="storage", type="varchar", required=false, description="物品存放处")
     * @ApiParams   (name="coin", type="varchar", required=false, description="金币量")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "发布成功！",
    "time": "1627553755",
    "data": {

    }
     * })
     */
    public function qssAdd()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token', $token)->value('id');
        $user = Db::name('personal')->where('id', $id)->find();
        $gameId = $this->request->post('id');
        $title = $this->request->post('title');
        $area_id = $this->request->post('area_id');
        $server_id = $this->request->post('server_id');
        $goods_type = $this->request->post('goods_type');
        $num = $this->request->post('num');
        $price = $this->request->post('price');
        $ask = $this->request->post('ask');
        $min = $this->request->post('min');
        $specs = $this->request->post('specs');
        $max = $this->request->post('max');
        $mobile = $this->request->post('mobile');
        $qq = $this->request->post('qq');
        $coin_type = $this->request->post('coin_type');
        $method = $this->request->post('method');
        $invite = $this->request->post('invite');
        $second_pwd = $this->request->post('second_pwd');
        $ware_pwd = $this->request->post('ware_pwd');
        $storage = $this->request->post('storage');
        $coin = $this->request->post('prices');
        if ($user['is_real'] != 1) {
            $this->error('未实名', [], 101);
        }
        $this->is_login($token);
        $rece_sn = 'O' . $this->getRefundSn($id);
        $typeName = Db::name('game_info')->where('id', $goods_type)->value('name');
        $gameName = Db::name('game')->where('id',$gameId)->value('game');
        $area = Db::name('game_info')->where('id', $area_id)->value('name');
        $server = Db::name('game_info')->where('id', $server_id)->value('name');
        $cross_id = Db::name('game_info')->where('id', $server_id)->value('cross');
        $cross = Db::name('gamecross')->where('id', $cross_id)->value('name');
        $image = Db::name('game')->where('id', $gameId)->value('image');

        $res = [
            'uid'   =>  $id,
            'class_type' => 1,
            'title'   =>  $title,
            'rece_sn'   =>   $rece_sn,
            'game_id'   =>  $gameId,
            'game'  =>  $gameName,
            'area_id'   =>  $area_id,
            'area'  =>  $area,
            'server_id' =>  $server_id,
            'server'    =>  $server,
            'cross_id'    =>  $cross_id,
            'cross'    =>  $cross,
            'goods_type'    =>  $goods_type,
            'type_name' =>  $typeName,
            'image' =>  $image,
            'num'   =>  $num,
            'price' =>  $price,
            'min'   =>  $min,
            'max'   =>  $max,
            'ask'   =>  $ask,
            'specs' =>  $specs,
            'mobile'    =>  $mobile,
            'qq'    =>  $qq,
            'coin_type' =>  $coin_type,
            'method' =>  $method,
            'invite' =>  $invite,
            'second_pwd' =>  $second_pwd,
            'ware_pwd' =>  $ware_pwd,
            'storage' =>  $storage,
            'createtime'    =>  time(),
            'status'    =>  1,
            'coin'    =>  $coin
        ];

        $data = Db::name('receiving')->insert($res);
        if ($data) {
            $this->success('发布成功！');
        } else {
            $this->error('发布失败！');
        }

    }



    /**
     * @ApiTitle    (收货页面展示)
     * @ApiSummary  (收货页面展示)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/receiveList)
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiParams   (name="type", type="varchar", required=true, description="物品类型id")
     * @ApiParams   (name="cross", type="varchar", required=true, description="跨区id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": [
    {
    "id": 5,
    "area": 区,
    "server": 服,
    "coin_type": 出货方式,
    "price": 获得金额,
    "num": 收货数量,
    "fc": 反除,
    "zc": 正除
    }
    ]
     * })
     */
    public function receiveList()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $cross = $this->request->post('cross');
        $map['status'] = 1;
        $map['num'] = ['gt',0];
        $map['class_type'] = 2;
        $map['game_id'] = $id;
        $map['goods_type'] = $type;
        $map['cross_id'] = $cross;
        $res = Db::name('receiving')
            ->field('id,class_type,area,server,coin_type,price,num,specs,max,min')
            ->where($map)
            ->order('createtime desc')
            ->select();
        foreach ($res as $k=>$v) {
            $coin = explode(',',$v['coin_type']);
            $coin_type = [];
            foreach ($coin as $kk=>$vv) {
                if ($vv == 1) {
                    $coin_type['a'] = '邮寄';
                } elseif ($vv == 2) {
                    $coin_type['b'] = '拍卖';
                } else {
                    $coin_type['c'] = '面对面';
                }
            }
            $res[$k]['coin_type'] = implode($coin_type,' ');
            if ($v['price'] >0 || $v['num']>0) {
                $res[$k]['fc'] =  round($v['price'],4);
                $res[$k]['zc'] =  round(1/$v['price'],4);
            }
        }
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (确认收货订单)
     * @ApiSummary  (确认收货订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/confirmReOrder)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="收货商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data":
    {
    "id": 1,
    "area": 区,
    "server": 服,
    "type_name": 物品类型,
    "coin_type": 交易方式,
    "price": 单价,
    "num": 数量,
    "specs": 单位,
    "image": "https://780g.oss-cn-zhangjiakou.aliyuncs.com/uploads/20211027/fcffcb9c226cc09d29b9efde2ebd015d.png",
    "max": 最大,
    "min": 最小,
    "fc": 反除,
    "zc": 正除
    }

     * })
     */
    public function confirmReOrder()
    {
        $gid = $this->request->post('id');
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $user = Db::name('personal')->where('id',$id)->find();
        $this->is_login($token);
        if ($user['is_real'] != 1) {
            $this->error('未实名',[],101);
        }
        $res = Db::name('receiving')
            ->field('id,title,area,server,cross_id,cross,type_name,coin_type,price,num,specs,image,max,min,is_safe,method')
            ->where('id',$gid)
            ->find();
        $res['serverAll'] = Db::name('game_info')
            ->field('id,name')
            ->where('cross',$res['cross_id'])
            ->where('status','normal')
            ->select();
        if ($res['price'] >0 || $res['num']>0) {
            $res['fc'] =  round($res['price'],4);
            $res['zc'] =  round(1/$res['price'],4);
        }
        $res['image'] = config('api_image_url') . $res['image'];
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (添加收货订单/发货订单)
     * @ApiSummary  (添加收货订单/发货订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/reOrder)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="商品id")
     * @ApiParams   (name="num", type="varchar", required=true, description="出货数量")
     * @ApiParams   (name="role_server", type="varchar", required=false, description="收货区服")
     * @ApiParams   (name="mobile", type="varchar", required=true, description="手机号")
     * @ApiParams   (name="qq", type="varchar", required=true, description="qq")
     * @ApiParams   (name="coin_type", type="varchar", required=true, description="出货方式 （1 邮寄交易 2 拍卖交易 3 面对面交易）")
     * @ApiParams   (name="arrival_type", type="varchar", required=true, description="到账方式（1 提现到支付宝/银行卡 2 余额）")
     * @ApiParams   (name="invite", type="varchar", required=true, description="推广码")
     * @ApiParams   (name="is_safe", type="varchar", required=false, description="1 无  2 30天  3 60天  4 90天")
     * @ApiParams   (name="role_account", type="varchar", required=true, description="收货角色")
     * @ApiParams   (name="role_level", type="varchar", required=true, description="收货等级")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下单成功！",
    "time": "1627553755",
    "data": {
    id：刚添加订单的id
    }
     * })
     */
    public function reOrder()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $user = Db::name('personal')->where('id',$id)->find();
        $gid = $this->request->post('id');
        $num = $this->request->post('num');
        $mobile = $this->request->post('mobile');
        $qq = $this->request->post('qq');
        $coin_type = $this->request->post('coin_type');
        $arrival_type = $this->request->post('arrival_type');
        $invite = $this->request->post('invite');
        $xyg = $this->request->post('is_safe');
        $role_server = $this->request->post('role_server');
        $roleServer = $role_server ? $role_server : '';
        $this->is_login($token);
        if ($user['is_real'] != 1) {
            $this->error('未实名',[],101);
        }
        $goods = Db::name('receiving')->where('id',$gid)->find();
        if ($goods['num'] - $num < 0) {
            $this->error('收货库存不足，请重新填写');
        }
        if ($num > $goods['num'] || $num < $goods['min']) {
            $this->error('请填写正确的数量');
        }
        if ($goods['status'] != 1) {
            $this->error('此商品已下架');
        }
        if ($goods['price'] > 0 || $goods['num']> 0) {
            $fc =  round($goods['price'],4);
        } else {
            $fc = 1;
        }
        if ($goods['uid'] == $id) {
            $this->error('您不可以购买自己的商品');
        }
//        if ($goods['uid'] == 12) {
//            $this->error('此商品已售罄');
//        }
        $fee = DB::name("server")->where(['game_id'=>$goods['game_id'],'safe'=>$goods['is_safe'],'method'=>$goods['method']])->find();
        if(!$fee){

            $fee = DB::name("server")->where(['game_id'=>$goods['game_id'],'safe'=>'2','method'=>$goods['method']])->find();
            if(!$fee){
                $fee = Db::name('server')->where('id',1)->find();
            }
        }
        if ($goods['class_type'] == 1) {
            $uid = $goods['uid'];
            $buy_id = $id;
            $order_user = 1;
            $role_level = $this->request->post('role_level');
            $role_account = $this->request->post('role_account');
            $is_safe = $xyg;
            //暂时写死，如果需要活的，直接去server表中xyg_fee即可
            if($goods['is_safe'] == 3){
                if($xyg){
                    switch ($xyg){
                        case 2:
                            $xyg_fee = '0.01';
                            break;
                        case 3:
                            $xyg_fee = '0.02';
                            break;
                        case 4:
                            $xyg_fee = '0.03';
                            break;
                        default:
                            $xyg_fee = '0';
                            break;
                    }
                }else{
                    $xyg_fee = '0';
                }

                $fee['service'] = $xyg_fee;
                $fees = $num * $goods['price'] * $fee['service'];
                if ($fees > 1) {
                    $amount = $num * $goods['price'] * (1+$fee['service']);
                } else {
                    if ($fee['service'] == '0') {
                        $amount = $num * $goods['price'];
                    } else {
                        $amount = $num * $goods['price'] + 1;
                    }
                }

            }else{
                //无服务 心意购费率是0
                $amount = $num*$goods['price'];
                $fee['service'] = 0;
            }
        } else {
            $uid = $id;
            $buy_id = $goods['uid'];
            $role_level = $goods['role_level'];
            $role_account = $goods['role_account'];
            $is_safe = 2;
            $order_user = 2;
            $amount = $num * $fc;
        }

        //查询白名单
        $user_mobile = DB::name("personal")->where("id",$goods['uid'])->value("mobile");
        $white_list = DB::name("order_whitelist")->where(['game_id'=>$goods['game_id'],'mobile'=>$user_mobile])->find();
        if($white_list){
            //卖家在白名单，则手续费为0
            $fee['service'] = 0;
            $fee['trade'] = 0;
            $amount = $goods['num']*$goods['price'];
        }
        $fee = $num * $goods['price'] * $fee['service'];
        $account = Db::name('personal')->where('id',$goods['uid'])->value('account');
        $ive_sn = 'I' . $this->getRefundSn($id);
        //添加分组，只有投诉咨询和一级客服可以分配
        $auth_group = DB::name("auth_group")->where(['name'=>['in',['投诉咨询','一级客服']]])->column('id');
        $auth_group_access = DB::name("auth_group_access")->where(['group_id'=>['in',$auth_group]])->column("uid");
        $admin_id = Db::name('admin')->where(['status'=>'normal','id'=>['in',$auth_group_access]])->orderRaw('Rand()')->value('id');
        $res = [
            'uid'   =>  $uid,
            'buyid'   =>  $buy_id,
            'order_user'   =>  $order_user,
            'account'   =>  $account,
            'order_sn'    =>  $ive_sn,
            'goods_id'   =>  $gid,
            'title' =>  $goods['title'],
            'image' =>  $goods['image'],
            'game_id'   =>  $goods['game_id'],
            'game'  =>  $goods['game'],
            'area_id'   =>  $goods['area_id'],
            'area'  =>  $goods['area'],
            'server_id' =>  $goods['server_id'],
            'server'    =>  $goods['server'],
            'cross_id'    =>  $goods['cross_id'],
            'cross'    =>  $goods['cross'],
            'goods_type'    =>  $goods['goods_type'],
            'type_name' =>  $goods['type_name'],
            'role_account' =>  $role_account,
            'role_level' =>  $role_level,
            'role_server'  =>  $roleServer,
            'specs' =>  $goods['specs'],
            'price' =>  $goods['price'],
            'buy_type' =>  $goods['method'],
            'num'   =>  $num,
            'prices' =>  $amount,
            'mobile' =>  $mobile,
            'qq' =>  $qq,
            'coin_type' =>  $coin_type,
            'arrival_type' =>  $arrival_type,
            'invite' =>  $invite,
            'status'    =>  1,
            'ontime'    =>  time(),
            'is_safe'   =>  $is_safe,
            'admin_id'    =>  $admin_id,
            'fee' => $fee,
        ];
        $data = Db::name('reorder')->insertGetId($res);
        if ($data) {
            if ($order_user == 2) {
                $send = new Send();
                $goods = Db::name('receiving')->where('id',$gid)->find();
                $send->send($goods['mobile'],8);
            }
            Db::name('receiving')->where('id',$gid)->setDec('num',$num);
            $this->success('下单成功',$data);
        } else {
            $this->error(__('下单失败'));
        }
    }



    /**
     * @ApiTitle    (订单发货/撤单)
     * @ApiSummary  (订单发货/撤单)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/flow)
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiParams   (name="type", type="varchar", required=true, description="7取消  3发货")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "操作成功",
    "time": "1627547401",

     * })

     */
    public function flow()
    {
        $id = $this->request->post('id');
        $order = Db::name('reorder')->where('id',$id)->find();
        if ($order['status'] != 2) {
            $this->error('此商品已发货,请误操作');
        }
        $type = $this->request->post('type');
        try {
            Db::startTrans();
            if ($type == 7) {
                $re['s1'] = Db::name('reorder')->where('id',$id)->update(['status'=>4]);
                $re['s2'] = Db::name('receiving')->where('id',$order['goods_id'])->setInc('num',$order['num']);
                $re['s3'] = Db::name('personal')->where('id',$order['buyid'])->setInc('balance',$order['prices']);
            } else {
                $re['s1'] = Db::name('reorder')->where('id',$id)->update(['status'=>'3','sendtime'=>time()]);
                $send = new Send();
                if ($order['order_user'] == 1) {
                    $goods = Db::name('personal')->where('id', $order['buyid'])->find();
                    $re['s2'] = $send->send($goods['mobile'], 5);
                } else {
                    $goods = Db::name('receiving')->where('id', $order['goods_id'])->find();
                    $re['s2'] = $send->send($goods['mobile'], 7);
                }
            }

            //任意一个表写入失败都会抛出异常：
            if (in_array('0', $re)) {
                throw new Exception('操作失败');
            }
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            //注意：我们做了回滚处理
            $this->error($e->getMessage());
        }
        $this->success('操作成功');
    }


    /**
     * @ApiTitle    (订单完成)
     * @ApiSummary  (订单完成)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/okReOrder)
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "操作成功",
    "time": "1627547401",

     * })

     */
    public function okReOrder()
    {
        $id = $this->request->post('id');
        $reorder = Db::name('reorder')->where('id',$id)->find();
        if ($reorder['status'] == 5) {
            $this->error('该订单已完成');
        }
        if ($reorder['status'] != 3) {
            $this->error('该订单未发货');
        }
        if ($reorder['order_user'] == 1) {
            $amount = $reorder['num'] * $reorder['price'] * 0.98;
        } else {
            $amount = $reorder['num'] * $reorder['price'] * 0.9952;
        }

        try {
            Db::startTrans();
            $re['s1'] = Db::name('reorder')->where('id',$id)->update(['status'=>5,'overtime'=>time()]);
            $ret = [
                'uid'   =>  $reorder['uid'],
                'log_sn'    =>  date('YmdHis') . rand(10000000,99999999),
                'money' =>  '+'.$amount,
                'type'  =>  2,
                'content'   =>  '出货订单交易',
                'createtime'    =>  time(),
                'arrivaltime'   =>  time(),
                'status'    =>  1
            ];
            $re['s2'] = Db::name('account_log')->insert($ret);
            $re['s3'] = Db::name('personal')->where('id',$reorder['uid'])->setInc('balance',$amount);

            //任意一个表写入失败都会抛出异常：
            if (in_array('0', $re)) {
                throw new Exception('确认收货失败');
            }
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            //注意：我们做了回滚处理，所以id为1039的数据还在
            $this->error($e->getMessage());
        }
        $this->success('确认收货成功');
    }



    /**
     * @ApiTitle    (订单支付页面)
     * @ApiSummary  (订单支付页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/confirmPay)
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data": {
    "id": 订单id,
    "order_sn": "订单号",
    "title": "标题",
    "server": "区服",
    "price": "价格"
    }
     * })

     */
    public function confirmPay()
    {
        $oid = $this->request->post('id');
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $res = Db::name('reorder')->field('id,order_sn,title,server,prices')->where('id',$oid)->find();
        $user = Db::name('personal')->where('id',$id)->find();
        $res['balance'] = $user['balance'];
        $res['mobile'] = $user['mobile'];
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (订单支付)
     * @ApiSummary  (订单支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/reOrderPay)
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiParams   (name="type", type="varchar", required=true, description="支付类型 微信:wechat 支付宝:alipay)
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data":
     * })

     */
    public function reOrderPay()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $reorder = Db::name('reorder')->where('id',$id)->find();


//        $amount = $reorder['prices'];
        $amount = 0.01;
        $orderid = $reorder['order_sn'];
        $title = "780g";
        $notifyurl = $this->request->root(true).'/api/pay/notifyre/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $orderid;
        $method = "web";
        $a = \addons\epay\library\Service::submitOrder($amount,$orderid,$type,$title,$notifyurl,$returnurl,$method);
        return $a;
    }



    /**
     * @ApiTitle    (收货展示列表)
     * @ApiSummary  (收货展示列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/receiptList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=true, description="1：收货中需求 2：已下架需求 3：进行中订单 4：已完结订单")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data": {
    "id": 商品id,
    "createtime": 1627547401,
    "game": "游戏",
    "area": "区",
    "server": "服",
    "title": "标题",
    "price": "收货单价"
    "num": "收货数量"
    "status": "状态  （1：上架  2：下架 3：已出售）"

    else

    "id": 订单id,
    "ontime": 1627547401,
    "order_sn": 订单号,
    "game": "游戏",
    "area": "区",
    "server": "服",
    "type_name": "物品类型",
    "title": "标题",
    "price": "订单金额"
    "sh_price": "收货单价"
    "over_price": "已收金额"
    "num": "收货数量"
    "over_num": "已收数量"
    "status": "状态  （1：上架  2：下架 3：已出售）"
    }
     * })

     */
    public function receiptList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $user = Db::name('personal')->where('id',$id)->find();
        if ($user['receset'] != 1) {
            $this->error('您还未开通此权限，请联系客服开通呢！亲！！');
        }
        if ($type == 3) {
            $res = Db::name('receiving')
                ->field('id,createtime,game,area,server,type_name,rece_sn,title,num,price,status')
                ->where('num','gt',0)
                ->where('uid',$id)
                ->where('status',1)
                ->where('class_type',2)
                ->order('createtime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['over_num'] = Db::name('reorder')->where('goods_id',$item['id'])->sum('num');
                    $item['over_price'] = Db::name('reorder')->where('goods_id',$item['id'])->sum('prices');
                    if ($item['price'] >0 || $item['num']>0) {
                        $item['prices'] = round($item['price']/$item['num'],4);
                    } else {
                        $item['prices'] = 0;
                    }
                    return $item;
                });
        } elseif ($type == 1) {
            $res = Db::name('receiving')
                ->field('id,createtime,game,area,server,type_name,rece_sn,title,num,price,status')
                ->where('num','gt',0)
                ->where('uid',$id)
                ->where('status',2)
                ->where('class_type',2)
                ->order('createtime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    if ($item['price'] >0 || $item['num']>0) {
                        $item['prices'] = round($item['price']/$item['num'],4);
                    } else {
                        $item['prices'] = 0;
                    }
                    $item['over_num'] = Db::name('reorder')->where('goods_id',$item['id'])->sum('num');
                    $item['over_price'] = Db::name('reorder')->where('goods_id',$item['id'])->sum('prices');
                    return $item;
                });
        } elseif ($type == 2) {
            $map['status'] = array('not in',array('4','5'));
            $map['buyid'] = $id;
            $res = Db::name('reorder')
                ->field('id,ontime,goods_id,order_sn,game,area,server,type_name,title,num,prices,coin_type,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    if ($item['prices'] >0 || $item['num']>0) {
                        $item['sh_price'] = round($item['prices']/$item['num'],4);
                    } else {
                        $item['sh_price'] = 0;
                    }
                    if ($item['coin_type'] == 1) {
                        $item['coin_type'] = '邮寄';
                    } elseif ($item['coin_type'] == 2) {
                        $item['coin_type'] = '拍卖';
                    } else {
                        $item['coin_type'] = '面对面';
                    }
                    $item['over_num'] = Db::name('reorder')->where('goods_id',$item['goods_id'])->sum('num');
                    $item['over_price'] = Db::name('reorder')->where('goods_id',$item['goods_id'])->sum('prices');
                    return $item;
                });
        } else {
            $res = Db::name('reorder')
                ->field('id,ontime,goods_id,order_sn,game,area,server,type_name,title,num,prices,coin_type,status')
                ->where('buyid',$id)
                ->where('status',5)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    if ($item['coin_type'] == 1) {
                        $item['coin_type'] = '邮寄';
                    } elseif ($item['coin_type'] == 2) {
                        $item['coin_type'] = '拍卖';
                    } else {
                        $item['coin_type'] = '面对面';
                    }
                    if ($item['prices'] >0 || $item['num']>0) {
                        $item['sh_price'] = round($item['prices']/$item['num'],4);
                    }

                    $item['over_price'] = Db::name('reorder')->where('goods_id',$item['goods_id'])->sum('prices');
                    $item['over_num'] = Db::name('reorder')->where('goods_id',$item['goods_id'])->sum('num');
                    return $item;
                });
        }
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (卖货展示列表)
     * @ApiSummary  (卖货展示列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/sellList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=true, description="1：全部订单 2：交易中 3：已完成 4：已取消")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data": {
    "0": {
    "id": 3,
    "image": "https://780g.oss-cn-zhangjiakou.aliyuncs.com",
    "ive_sn": "I202105085571821301001000",
    "createtime": 1640336935,
    "type_name": "金币",
    "game": "地下城与勇士",
    "area": "广东区",
    "server": "广东1区",
    "num": 0,
    "price": "0.00"
    },

    "okorder": 成交订单笔数,
    "okprice": 成交订单金额
    }
     * })

     */
    public function sellList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $map['uid'] = $id;
        $map['order_user'] = 2;
        if ($type == 1) {
            $res = Db::name('reorder')
                ->field('id,title,specs,image,order_sn,ontime,type_name,game,area,server,num,prices as price')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 2) {
            $map['status'] = array('not in',array('1','4','5'));
            $res = Db::name('reorder')
                ->field('id,title,specs,image,order_sn,ontime,type_name,game,area,server,num,prices as price')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 3) {
            $map['status'] = 5;
            $res = Db::name('reorder')
                ->field('id,title,specs,image,order_sn,ontime,type_name,game,area,server,num,prices as price')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 4) {
            $map['status'] = 4;
            $res = Db::name('reorder')
                ->field('id,title,specs,image,order_sn,ontime,type_name,game,area,server,num,prices as price')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } else {
            $map['status'] = 2;
            $res = Db::name('reorder')
                ->field('id,title,specs,image,order_sn,ontime,type_name,game,area,server,num,prices as price')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        }
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (vip--我购买的商品)
     * @ApiSummary  (vip--我购买的商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/imBuy)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=true, description="1：全部订单 2：交易中 3：已完成 4：已取消 5：待支付")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data": {
    "0": {
    "id": 3,
    "image": "https://780g.oss-cn-zhangjiakou.aliyuncs.com",
    "ive_sn": "I202105085571821301001000",
    "createtime": 1640336935,
    "type_name": "金币",
    "game": "地下城与勇士",
    "area": "广东区",
    "server": "广东1区",
    "num": 0,
    "price": "0.00"
    },

    "okorder": 成交订单笔数,
    "okprice": 成交订单金额
    }
     * })

     */
    public function imBuy()
    {
        $type = $this->request->post('type');
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $map['buyid'] = $id;
        $map['order_user'] = 1;
        if ($type == 1) {
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 2) {
            $map['status'] = array('not in',array('1','4','5'));
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 3) {
            $map['status'] = 5;
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 4) {
            $map['status'] = 4;
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } else {
            $map['status'] = 1;
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        }
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (vip--我卖出的订单)
     * @ApiSummary  (vip--我卖出的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/imSell)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=true, description="1：全部订单 2：交易中 3：已完成 4：已取消")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1627547401",
    "data": {
    "0": {
    "id": 3,
    "image": "https://780g.oss-cn-zhangjiakou.aliyuncs.com",
    "ive_sn": "I202105085571821301001000",
    "createtime": 1640336935,
    "type_name": "金币",
    "game": "地下城与勇士",
    "area": "广东区",
    "server": "广东1区",
    "num": 0,
    "price": "0.00"
    },

    "okorder": 成交订单笔数,
    "okprice": 成交订单金额
    }
     * })

     */
    public function imSell()
    {
        $type = $this->request->post('type');
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $map['uid'] = $id;
        $map['order_user'] = 1;
        if ($type == 1) {
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 2) {
            $map['status'] = array('not in',array('1','4','5'));
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } elseif ($type == 3) {
            $map['status'] = 5;
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } else {
            $map['status'] = 4;
            $res = Db::name('reorder')
                ->field('id,image,specs,title,order_sn,ontime,type_name,game,area,server,num,prices as amount,price as qss_price,status')
                ->where($map)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        }
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (余额支付)
     * @ApiSummary  (余额支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/coinPay)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiParams   (name="mobile", type="varchar", required=true, description="手机号")
     * @ApiParams   (name="captcha", type="varchar", required=true, description="验证码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "支付成功",
    "time": "1627547401",
    "data": {

    }
     * })

     */
    public function coinPay()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $ids = $this->request->post('id');
        file_put_contents('yue.log',$ids);
        $user = Db::name('personal')->where('id',$id)->find();
        $balance = $user['balance'];
        $order = Db::name('reorder')->where('id',$ids)->find();
        if ($balance - $order['prices'] < 0) {
            $this->error('余额不足');
        }
        if ($order['status'] != 1) {
            $this->error('订单已支付');
        }
        if ($order['status'] == 4) {
            $this->error('订单已超时,请重新下单');
        }
        $captcha = $this->request->post('captcha');
        $mobile = $this->request->post('mobile');
        if (!Sms::check($mobile, $captcha, 'coinpay')) {
            $this->error(__('请填写正确的验证码'));
        }
        try {
            Db::startTrans();
            $ret1 = [
                'uid'   =>  $user['id'],
                'log_sn'    =>  date('YmdHis') . rand(10000000,99999999),
                'money' =>  '-'.$order['prices'],
                'type'  =>  1,
                'content'   =>  '消费',
                'createtime'    =>  time(),
                'arrivaltime'   =>  time(),
                'status'    =>  1
            ];
            $re['s1'] = Db::name('reorder')->where('id',$ids)->update(['status'=>2,'paytime'=>time(),'payway'=>'余额']);
            $re['s2'] = Db::name('account_log')->insert($ret1);
            $re['s3'] = Db::name('personal')->where('id',$user['id'])->setDec('balance',$order['prices']);
            $send = new Send();
            if ($order['order_user'] == 1) {
                $goods = Db::name('receiving')->where('id',$order['goods_id'])->find();
                $re['s4'] = $send->send($goods['mobile'],6);
            } else {
                $goods = Db::name('personal')->where('id',$order['uid'])->find();
                $re['s4'] = $send->send($goods['mobile'],9);
            }
            //任意一个表写入失败都会抛出异常：
            if (in_array('0', $re)) {
                throw new Exception('支付失败');
            }
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            //注意：我们做了回滚处理，所以id为1039的数据还在
            $this->error($e->getMessage());
        }
        $this->success('支付成功');
    }




    /**
     * @ApiTitle    (vip商品列表)
     * @ApiSummary  (vip商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/vipList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=true, description="游戏id")
     * @ApiParams   (name="type", type="varchar", required=false, description="游戏类型id")
     * @ApiParams   (name="cross", type="varchar", required=false, description="跨区id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 1,
    "goods_sn": "商品单号",
    "uid": 1,
    "buy_type": 1,
    "gameid": 1,
    "gamename": "王者荣耀",
    "goods_type": 1,
    "title": "王者荣耀208英雄300皮",
    "price": "1088.00",
    "havetime": 1634011200,
    "stock": null,
    "area": null,
    "status": 0,
    "createtime": 1634022470

     * })
     */
    public function vipList()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        $cross = $this->request->post('cross');
        if (empty($type)) {
            $map['goods_type'] = Db::name('game_info')->field('id,game')->where('game_id',$id)->where('gt_id',5)->value('id');
        } else {
            $map['goods_type'] = $type;
        }
        $map['num'] = ['gt',0];
        $map['game_id'] = $id;
        $map['status'] = 1;
        $map['class_type'] = 1;
        $map['cross_id'] = $cross;
        $res = Db::name('receiving')
            ->field('id,uid,class_type,title,server,cross,type_name,num,price,max,min,specs,coin')
            ->where($map)
            ->order('createtime desc')
            ->paginate(15,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                if ($item['price'] >0 || $item['num']>0) {
                    $item['fc'] =  round($item['price'],4);
                    $item['zc'] =  round(1/$item['price'],2);
                }
                return $item;
            });
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (商品下架)
     * @ApiSummary  (商品下架)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/undercarriage)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="下架商品的id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下架成功",
    "time": "1627553755",

     * })
     */
    public function undercarriage()
    {
        $token = $this->request->header('token');
        $this->is_login($token);
        $id = $this->request->post('id');
        if (empty($id)) {
            $this->success('下架失败');
        }
        $ids = explode(',',$id);
        foreach ($ids as $k=>$v) {
            Db::name('receiving')->where('id',$v)->update(['status'=>2]);
        }
        $this->success('下架成功');
    }


    /**
     * @ApiTitle    (商品上架)
     * @ApiSummary  (商品上架)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/carriage)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="下架商品的id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下架成功",
    "time": "1627553755",

     * })
     */
    public function carriage()
    {
        $id = $this->request->post('id');
        $token = $this->request->header('token');
        $this->is_login($token);
        if (empty($id)) {
            $this->success('下架失败');
        }
        $ids = explode(',',$id);
        foreach ($ids as $k=>$v) {
            Db::name('receiving')->where('id',$v)->update(['status'=>1,'createtime'=>time()]);
        }
        $this->success('上架成功');
    }


    /**
     * @ApiTitle    (订单管理订单取消)
     * @ApiSummary  (订单管理订单取消)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/flows)
     * @ApiParams   (name="id", type="varchar", required=true, description="订单id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "操作成功",
    "time": "1627547401",

     * })

     */
    public function flows()
    {
        $id = $this->request->post('id');
        $order = Db::name('order')->where('id',$id)->find();
        try {
            Db::startTrans();

            $re['s1'] = Db::name('reorder')->where('id',$id)->update(['status'=>4]);
            $re['s2'] = Db::name('receiving')->where('id',$order['goods_id'])->setInc('num',$order['num']);
            if ($order['status'] == 2) {
                $re['s3'] = Db::name('personal')->where('id',$order['buyid'])->setInc('balance',$order['prices']);
            }


            //任意一个表写入失败都会抛出异常：
            if (in_array('0', $re)) {
                throw new Exception('操作失败');
            }
            // 提交事务
            Db::commit();
            throw new Exception('操作成功');
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            //注意：我们做了回滚处理
            $this->error($e->getMessage());
        }
    }




    /**
     * @ApiTitle    (我的商品)
     * @ApiSummary  (我的商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Receive/sellGoods)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:上架 2:用户下架")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    "id": 3,
    "goods_sn": 编号,
    "type_name": 类型名
    "createtime": 创建时间
    "price": 价格
    "image": 图片
    "title": 标题
    "gamename": 游戏名
    "area": 区服
    "stock": 库存
    "status": 1:上架 2:用户下架 3:审核失败 4:审核中 5:平台下架
    "auth_status": 0未提交1审核中2已认证3审核失败
    "havetime": 还有多少天
    "reason":原因
    }
     * })
     */
    public function sellGoods()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $data = Db::name('receiving')
            ->field('id,rece_sn as goods_sn,type_name,createtime,price,image,game_id,title,game,area,server,num as stock,status,is_safe,specs,coin')
            ->where('class_type',1)
            ->where('uid',$id)
            ->where('num','gt',0)
            ->where('status',$type)
            ->order('createtime desc')
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                $item['image'] = config('api_image_url') . $item['image'];
                return $item;
            });
        $this->success('ok',$data);
    }



}
